import path from 'path'
import fs from 'fs'
import { platformList } from '@/views/common.js'
const remote = require('@electron/remote')
// const oldGroupPath = path.join(remote.app.getPath('userData'), '/group.json')
const oldGroupPath = path.join(remote.app.getPath('userData'), '/group.json')
const groupPath = path.join(remote.app.getPath('userData'), '/all-group-list.json')

// eslint-disable-next-line no-unused-vars
// const test = () => {
//   const d = [{ code: 1, message: '分组1', alias: '测试呢' },
//     { code: 2, message: '分组2', alias: '分组2' },
//     { code: 3, message: '分组3', alias: '分组3' },
//     { code: 4, message: '分组4', alias: '分组4' },
//     { code: 5, message: '分组5', alias: '分组5' },
//     { code: 6, message: '分组6', alias: '分组6' },
//     { code: 7, message: '分组7', alias: '分组7' },
//     { code: 8, message: '分组8', alias: '分组8' },
//     { code: 9, message: '分组9', alias: '分组9' },
//     { code: 10, message: '分组10', alias: '分组10' },
//     { code: 11, message: '分组11', alias: '分组11' },
//     { code: 12, message: '分组12', alias: '分组12' }]
//   fs.writeFileSync(oldGroupPath, JSON.stringify(d))
// }

// 兼容旧版本的问题
// eslint-disable-next-line no-unused-vars
const tryRemove = () => {
  try {
    const data = fs.readFileSync(oldGroupPath)
    if (data) {
      const result = JSON.parse(data)
      if (Object.prototype.toString.call(result) === '[object Array]') {
        const obj = {}
        platformList.forEach(item => {
          const { code } = item
          obj[code] = [...defaultGroupList]
        })
        obj[1] = result
        fs.writeFileSync(groupPath, JSON.stringify(obj))
      }
    }
    fs.unlinkSync(oldGroupPath)
  } catch (e) {
    console.log(e)
  }
}

export const defaultGroupList = [
  { code: 1, message: '分组1', alias: '分组1' },
  { code: 2, message: '分组2', alias: '分组2' },
  { code: 3, message: '分组3', alias: '分组3' },
  { code: 4, message: '分组4', alias: '分组4' },
  { code: 5, message: '分组5', alias: '分组5' },
  { code: 6, message: '分组6', alias: '分组6' },
  { code: 7, message: '分组7', alias: '分组7' },
  { code: 8, message: '分组8', alias: '分组8' },
  { code: 9, message: '分组9', alias: '分组9' },
  { code: 10, message: '分组10', alias: '分组10' },
  { code: 11, message: '分组11', alias: '分组11' },
  { code: 12, message: '分组12', alias: '分组12' }
]

export const defaultAllGroupList = [
  {
    code: -2,
    message: '所有'
  },
  {
    code: -1,
    message: '未分组'
  },
  ...defaultGroupList
]

export const getGroupListByPlatform = (platform) => {
  const all = getAllPlatformGroupList()
  // eslint-disable-next-line no-unused-vars
  const data = all ? (all[platform] || []) : []
  console.log(data)
  return [
    {
      code: -2,
      message: '所有',
      alias: '所有'
    },
    {
      code: -1,
      message: '未分组',
      alias: '未分组'
    },
    ...data
  ]
}

export const setGroupList = (platform, data) => {
  try {
    const obj = getAllPlatformGroupList() || {}
    obj[platform] = data
    console.log(obj)
    fs.writeFileSync(groupPath, JSON.stringify(obj))
  } catch (e) {
    console.log('write group data error')
    console.log(e)
  }
}

export const initGroupAllGroupList = () => {
  const obj = {}
  platformList.forEach(item => {
    const { code } = item
    obj[code] = [...defaultGroupList]
  })
  fs.writeFileSync(groupPath, JSON.stringify(obj))
}

export const getAllPlatformGroupList = () => {
  try {
    const data = fs.readFileSync(groupPath, 'utf-8')
    if (data) {
      return JSON.parse(data)
    }
    // return JSON.parse(data)
  } catch (e) {
    console.log('get group data error')
    console.log(e)
  }
  return null
}

export const getGroupList = (key) => {
  try {
    const obj = getAllPlatformGroupList()
    if (obj) {
      return obj[key]
    }
  } catch (e) {
    console.log('get group data error')
    console.log(e)
  }
  return []
}

// 有些平台是后面新增的，没有分组功能，要加上update的功能
export const tryUpdatePlatformGroup = () => {
  const all = getAllPlatformGroupList() || {}
  for (let i = 0; i < platformList.length; i++) {
    const platform = platformList[i]
    const { code } = platform
    if (!all[code]) {
      setGroupList(code, [...defaultGroupList])
    }
  }
}

tryRemove()
